package cn.test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName Card
 * @Description TODO
 * @Author 郑万富
 * @Date 2020/11/20 20:21
 * @Version 1.0
 */
public class LoveCard {

    public static void createAsciiPic(final String path) throws FileNotFoundException {
        //final String base = "@#&$%*o!;.";// 字符串由复杂到简单
        final String base = "KSPksp;.";
        File outputFile = new File("./output.txt");
        PrintWriter pr = new PrintWriter(outputFile);

        try {
            final BufferedImage image = ImageIO.read(new File(path));
            System.out.println("W:"+image.getWidth()+" H:"+image.getHeight());
            for (int y = 0; y < image.getHeight(); y += 2) {
                for (int x = 0; x < image.getWidth(); x++) {
                    final int pixel = image.getRGB(x, y);
                    final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
                    final float gray = 0.299f * r + 0.578f * g + 0.114f * b;
                    final int index = Math.round(gray * (base.length() + 1) / 255);
                    pr.print(index >= base.length() ? " " : String.valueOf(base.charAt(index)));

                }
                pr.println();
                pr.flush();
//                System.out.println();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args []) throws FileNotFoundException {
        LoveCard.createAsciiPic("G:\\Java\\code\\JavaEE\\experiment03\\img\\陈都灵.jpg");
    }

}
